package com.yanqu.road.server.gameplayer.module.cricket.test;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DBManager;
import com.yanqu.road.dao.db.DatabaseMgr;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.entity.cricket.CricketData;
import com.yanqu.road.entity.cricket.CricketSkill;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.cricket.CricketProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.cricket.CricketMgr;
import com.yanqu.road.server.gameplayer.module.cricket.CricketModule;
import com.yanqu.road.server.gameplayer.module.cricket.battle.CricketBattle;
import com.yanqu.road.server.gameplayer.module.cricket.model.CricketBattleData;
import com.yanqu.road.server.gameplayer.module.cricket.model.CricketGameResult;
import com.yanqu.road.server.logic.smallgame.stage.GridSwitchStage;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.pb.CricketPb;
import com.yanqu.road.utils.CsvHelper;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class Cricket {

    public static Cricket getInstance() {
        return new Cricket();
    }

    public Cricket() {

    }

    public void run(Path path) {
        long a = 1;
        long b = 2;

        String pathStr = path.getParent().toString();
        String fileName = path.getFileName().toString();

        List<CricketBattleTest> cricketBattleTestList = new ArrayList<>();
        String selectSql = "SELECT * FROM cricket_battle_test";
        DBManager db = DatabaseMgr.getDbManager();
        ResultSet rs = null;
        PreparedStatement call = null;
        try {
            db.setConnName(DbNameStrategy.getUserDb());
            call = db.getConnection().prepareCall(selectSql);
            rs = call.executeQuery();
            while (rs.next()) {

                CricketBattleTest cricketBattleTest = CricketBattleTest.getInstance();
                cricketBattleTest.setId(rs.getLong("id"));
                cricketBattleTest.setA_cricket_id(rs.getInt("a_cricket_id"));
                cricketBattleTest.setA_cricket_p1(rs.getInt("a_cricket_p1"));
                cricketBattleTest.setA_cricket_p2(rs.getInt("a_cricket_p2"));
                cricketBattleTest.setA_cricket_skills(rs.getString("a_cricket_skills"));
                cricketBattleTest.setB_cricket_id(rs.getInt("b_cricket_id"));
                cricketBattleTest.setB_cricket_p1(rs.getInt("b_cricket_p1"));
                cricketBattleTest.setB_cricket_p2(rs.getInt("b_cricket_p2"));
                cricketBattleTest.setB_cricket_skills(rs.getString("b_cricket_skills"));
                cricketBattleTestList.add(cricketBattleTest);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            db.closeConnection(call, rs);
        }

        for (CricketBattleTest cricketBattleTest : cricketBattleTestList) {
            //A玩家
            CricketData cricketData = CricketMgr.initCricket(cricketBattleTest.getA_cricket_id(), a);
            cricketData.getInitSkillList().clear();
            cricketData.getSkillList().clear();
            cricketData.setTitle(cricketBattleTest.getA_cricket_p1());
            cricketData.setLittleTitle(cricketBattleTest.getA_cricket_p2());
            String[] split = cricketBattleTest.getA_cricket_skills().trim().split("\\|");
            for (String s : split) {
                int skillId = Integer.parseInt(s);
                if (skillId == 0) {
                    continue;
                }
                CricketSkill cricketSkill = new CricketSkill(skillId);
                cricketData.getInitSkillList().add(cricketSkill);
            }
            CricketMgr.calcProperty(cricketData);
            CricketProto.CricketMatchDataTemp.Builder cricketMatchDataTemp = CricketPb.parseCricketMatchDataTemp(cricketData);
            for (CricketSkill cricketSkill : cricketData.getInitSkillList()) {
                CricketProto.CricketSkillTempMsg.Builder cricketSkillTemp = CricketPb.parseCricketSkillTemp(cricketSkill);
                cricketMatchDataTemp.addBattleSkill(cricketSkillTemp);
            }
            CricketBattleData cricketBattleData = CricketBattleData.getInstance(cricketMatchDataTemp.build());

            //B玩家
            CricketData cricketData1 = CricketMgr.initCricket(cricketBattleTest.getB_cricket_id(), b);
            cricketData1.getInitSkillList().clear();
            cricketData1.getSkillList().clear();
            cricketData1.setTitle(cricketBattleTest.getB_cricket_p1());
            cricketData1.setLittleTitle(cricketBattleTest.getB_cricket_p2());
            String[] split1 = cricketBattleTest.getB_cricket_skills().trim().split("\\|");
            for (String s : split1) {
                int skillId = Integer.parseInt(s);
                if (skillId == 0) {
                    continue;
                }
                CricketSkill cricketSkill = new CricketSkill(skillId);
                cricketData1.getInitSkillList().add(cricketSkill);
            }
            CricketMgr.calcProperty(cricketData1);
            CricketProto.CricketMatchDataTemp.Builder cricketMatchDataTemp1 = CricketPb.parseCricketMatchDataTemp(cricketData1);
            for (CricketSkill cricketSkill : cricketData1.getInitSkillList()) {
                CricketProto.CricketSkillTempMsg.Builder cricketSkillTemp = CricketPb.parseCricketSkillTemp(cricketSkill);
                cricketMatchDataTemp1.addBattleSkill(cricketSkillTemp);
            }
            CricketBattleData cricketBattleData1 = CricketBattleData.getInstance(cricketMatchDataTemp1.build());
            CricketBattle cricketBattle = new CricketBattle();
            List<Integer> avgTurns = new ArrayList<>();
            List<Long> aSurrenderAvgHp = new ArrayList<>();
            List<Long> bSurrenderAvgHp = new ArrayList<>();


//            if (cricketData.getCricketId() == 240415) {
//                System.out.println(cricketData);
//            }

            for (int i = 0; i < 1000; i++) {
                CricketGameResult cricketGameResult = cricketBattle.gameRound(cricketBattleData, cricketBattleData1);
                if (cricketGameResult.getCricketGameRoundBuilder().getWinUserId() == a) {
                    cricketBattleTest.setA_win_num(cricketBattleTest.getA_win_num() + 1);
                }
                if (cricketGameResult.getMaxRound() >= GameConfig.CRICKET_FIGHT_ROUND_MAX) {
                    cricketBattleTest.setExceed_turn_times(cricketBattleTest.getExceed_turn_times() + 1);
                } else {
                    avgTurns.add(cricketGameResult.getMaxRound());
                }

                cricketBattleTest.setA_trigger_gnash(cricketBattleTest.getA_trigger_gnash() + cricketGameResult.getBattleData().getTotalFightScareNum());
                cricketBattleTest.setB_trigger_gnash(cricketBattleTest.getB_trigger_gnash() + cricketGameResult.getBattleData1().getTotalFightScareNum());
                if (cricketGameResult.getBattleData().isSurrender()) {
                    cricketBattleTest.setA_trigger_surrender(cricketBattleTest.getA_trigger_surrender() + 1);
                    double p = (double) cricketGameResult.getBattleData().getHp() / (double) cricketGameResult.getBattleData().getMaxHp() * (double) 1000;
                    aSurrenderAvgHp.add((long) p);
                }
                if (cricketGameResult.getBattleData1().isSurrender()) {
                    cricketBattleTest.setB_trigger_surrender(cricketBattleTest.getB_trigger_surrender() + 1);
                    double p = (double) cricketGameResult.getBattleData1().getHp() / (double) cricketGameResult.getBattleData1().getMaxHp() * (double) 1000;
                    bSurrenderAvgHp.add((long) p);
                }
            }
            if (avgTurns.size() > 0) {
                cricketBattleTest.setUn_exceed_avg_turns((int) avgTurns.stream().mapToDouble(i -> i).average().getAsDouble());

            }
            if (aSurrenderAvgHp.size() > 0) {
                cricketBattleTest.setA_trigger_surrender_avg_hp((int) aSurrenderAvgHp.stream().mapToDouble(i -> i).average().getAsDouble());
            }

            if (bSurrenderAvgHp.size() > 0) {
                cricketBattleTest.setB_trigger_surrender_avg_hp((int) bSurrenderAvgHp.stream().mapToDouble(i -> i).average().getAsDouble());
            }
        }

        CsvHelper.createCsv(CricketBattleTest.class, cricketBattleTestList, pathStr, fileName);
    }
}
